<template>
  <el-dialog
    center
    :visible="dialogVisible "
    width="60%"
    title="选择设备"
    append-to-body
    :before-close="handleClose"
    @closed="handleClose"
    :close-on-click-modal="false"
  >
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
    >
      <el-form-item label="工厂">
        <el-select v-model="queryParams.companyCode" @change="getList">
          <el-option v-for="item in factoryList" :label="item.companyName" :value="item.companyId" :key="item.companyId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData"
      style="width: 100%;"
      height="500"
    >
      <el-table-column
        label="租赁物设备名称"
        align="center"
        prop="equipmentName"
        :show-overflow-tooltip="true"
        min-width="150"
      />
      <el-table-column
        label="所属分类"
        align="center"
        prop="equipmentType"
        :show-overflow-tooltip="true"
        min-width="150"
      />
      <el-table-column
        label="规格型号"
        align="center"
        prop="equipmentModel"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="厂商"
        align="center"
        prop="company"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="单位"
        align="center"
        prop="unit"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="指导价（万元）"
        align="center"
        prop="price"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ formatReceiveAmount(scope.row.price / 10000) }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- icon="el-icon-edit" -->
          <el-button
            size="mini"
            type="text"
            @click="handleOption(scope.row)"
          >选择
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-dialog>
</template>

<script>
import { equipmentList, getCompanyList } from '@/api/processManage/approveForms'
import { formatMoney } from '@/utils/utils';
export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentName: '',
        companyCode: null
      },
      factoryList: [],
      total: 0,
      loading: false,
    };
  },
  methods: {
    formatReceiveAmount(amount) {
      return formatMoney(amount);
    },
    async sync() {
      this.dialogVisible = true;
      if (this.factoryList.length === 0) {
        this.getFactoryList()
      }
    },
    // 列表
    getFactoryList() {
      getCompanyList("factory").then(res => {
        this.factoryList = res.rows
      })
    },
    handleClose() {
      this.dialogVisible = false;
    },
    handleOption(row) {
      this.$emit('ok', row)
      this.handleClose();
    },
    handleQuery() {
      if (!this.queryParams.companyCode) {
        this.$message.error("工厂不能为空")
        return
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams.equipmentName = ''
      this.handleQuery()
    },
    // 选择工厂后查询设备列表
    getList() {
      this.loading = true
      equipmentList(this.queryParams).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false
      })
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
